package com.example.runningman.friend;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.runningman.MainActivity;
import com.example.runningman.R;
import com.example.runningman.adapter.Art_adapter;
import com.example.runningman.bean.Art;
import com.example.runningman.bean.User;
import com.example.runningman.user.LoginActivity;
import com.example.runningman.util.SPUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class SelfActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    ImageButton imageButton;
    Art_adapter adapter;
    String useid;
    LinearLayoutManager layoutManager;
    static List<Art> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler = new Mhandler(this);
        setContentView(R.layout.activity_self);
        recyclerView = findViewById(R.id.self);
        adapter = new Art_adapter(list, SelfActivity.this);
        layoutManager = new LinearLayoutManager(SelfActivity.this);
        useid=SPUtil.getInstance(getApplicationContext()).getString("useid");
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        imageButton=findViewById(R.id.back33);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SelfActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);
        postRequest(useid);
    }
    private static Gson gson=new Gson();
    private Mhandler mHandler;

    final OkHttpClient client = new OkHttpClient();
    private class Mhandler extends Handler {
        private WeakReference<SelfActivity> mActivity;

        public Mhandler(SelfActivity activity){
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String ReturnMessage = (String) msg.obj;
                Log.i("获取的返回信息","+"+ReturnMessage+"+");
                list=gson.fromJson(ReturnMessage, new TypeToken<List<Art>>(){}.getType());
                adapter.refresh(list);
                /***
                 * 在此处可以通过获取到的Msg值来判断
                 * 给出用户提示注册成功 与否，以及判断是否用户名已经存在
                 */

            }
        }
    }

    /**
     * post请求后台
     */
    private void postRequest(String useid)  {
        //建立请求表单，添加上传服务器的参数
        RequestBody formBody = new FormBody.Builder()
                .add("useid",useid)
                .build();
        //发起请求
        final Request request = new Request.Builder()
                .url("http://120.26.177.58:8080/AndroidServer01/getSelfArt?")
                .post(formBody)
                .build();
        //新建一个线程，用于得到服务器响应的参数
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    //回调
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        //将服务器响应的参数response.body().string())发送到hanlder中，并更新ui
                        mHandler.obtainMessage(1, response.body().string()).sendToTarget();

                    } else {
                        throw new IOException("Unexpected code:" + response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
